let sys_sequelize = require("../../dao/sequelize/sys");
let login_util = require("../../util/login_util");
let rbac_service = require("../../service/rbac_service");
let crypto = require("crypto");
let strings = require("../../strings");
let permissions = require("../../config/permissions");
const Op = require("sequelize").Op;

module.exports = [
  {
    method: "get",
    permissions: [permissions.rbac],
    path: "/sys/getUsers",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.getUsers(
        ctx.request.query.filterKey,
        ctx.request.query.size,
        ctx.request.query.page
      );
      return next();
    }
  },
  {
    method: "post",
    permissions: [permissions.rbac],
    path: "/sys/addUser",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.addUser(ctx.request.body.data);
      return next();
    }
  },
  {
    method: "post",
    permissions: [permissions.rbac],
    path: "/sys/editUser",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.editUser(ctx.request.body.data);
      return next();
    }
  },
  {
    method: "post",
    permissions: [permissions.rbac],
    path: "/sys/removeUser",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.removeUser(ctx.request.body.data.id);
      return next();
    }
  },
  {
    method: "get",
    permissions: [permissions.rbac],
    path: "/sys/getRoles",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.getRoles();
      return next();
    }
  },
  {
    method: "post",
    permissions: [permissions.rbac],
    path: "/sys/editRole",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.editRole(ctx.request.body.data);
      return next();
    }
  },
  {
    method: "post",
    permissions: [permissions.rbac],
    path: "/sys/removeRole",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.removeRole(
        ctx.request.body.data.code
      );
      return next();
    }
  },
  {
    method: "get",
    permissions: [permissions.rbac],
    path: "/sys/getPermissions",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.getPermissions();
      return next();
    }
  },
  {
    method: "get",
    permissions: [permissions.rbac],
    path: "/sys/getUserRoles",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.getUserRoles(ctx.request.query.id);
      return next();
    }
  },
  {
    method: "post",
    permissions: [permissions.rbac],
    path: "/sys/editUserRoles",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.editUserRoles(
        ctx.request.body.data.id,
        ctx.request.body.data.roles
      );
      return next();
    }
  },
  {
    method: "get",
    permissions: [permissions.rbac],
    path: "/sys/getRolePermissions",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.getRolePermissions(
        ctx.request.query.code
      );
      return next();
    }
  },
  {
    method: "post",
    permissions: [permissions.rbac],
    path: "/sys/editRolePermissions",
    async callback(ctx, next) {
      ctx.pass_result = await rbac_service.editRolePermissions(
        ctx.request.body.data.code,
        ctx.request.body.data.permissions
      );
      return next();
    }
  }
];
